package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/design-spreadsheet/description/?envType=daily-question&envId=2025-09-19
 * @create 2025/9/19 20:55
 * @since JDK17
 */

public class Spreadsheet {
    int[][] arr;
    public Spreadsheet(int rows) {
        arr = new int[rows + 1][27];
    }
    int[] get(String cell) {
        int col = cell.charAt(0) - 'A' + 1, row = Integer.valueOf(cell.substring(1));
        return new int[]{row, col};
    }
    int val(String cell) {
        if (Character.isUpperCase(cell.charAt(0))) { // 单元格
            int col = cell.charAt(0) - 'A' + 1, row = Integer.valueOf(cell.substring(1));
            return arr[row][col];
        } else {
            return Integer.valueOf(cell);
        }
    }
    public void setCell(String cell, int value) {
        int[] p = get(cell);
        arr[p[0]][p[1]] = value;
    }

    public void resetCell(String cell) {
        setCell(cell, 0);
    }

    public int getValue(String formula) {
        String[] split = formula.substring(1).split("\\+");
        return val(split[0]) + val(split[1]);
    }
}
